package io.datastream;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Test;

public class ReaderWriterDemo {
	@Test
	public void read() {
		try {
			//1.
			FileReader fr = new FileReader("b.txt");
			//2.读一个一个字符
//			int ch;
//			while ((ch = fr.read()) != -1) {
//				System.out.print((char)ch);
//			}
			long size = Files.size(Paths.get("b.txt"));
			//2. 全部读到char数组
			char[] arr = new char[(int) size/3];
			fr.read(arr);
			
			// 3.关闭
			fr.close();
			System.out.println(arr);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void write() {
		try {
			FileWriter fw = new FileWriter("c.txt",true);
			//1.写一个字符
			fw.write('哈');
				//写一个字符数组
			fw.write("hello".toCharArray());
				//写一个字符串
			fw.write("world");
			
			//2.刷新
			fw.flush();
			
			//关闭
			fw.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
